package com.gzbugu.bugusys.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.RequestAware;

import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.util.IHQLQuery;
import com.gzbugu.bugusys.vo.Staff;
import com.opensymphony.xwork2.ActionSupport;

import com.gzbugu.bugusys.bean.PageModel;

public class FindStaffAction extends ActionSupport implements RequestAware {

	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;
	@Resource(name = "hQLQuery")
	IHQLQuery hqlQuery;

	private Map<String, Object> request;
	private List errorList;
	private String staffId;
	private PageModel page;
	private Staff staff = null;
	private List staffList = new ArrayList();
	private Set familySet = new HashSet();
	private Set educationSet = new HashSet();
	private Set experienceSet = new HashSet();
	private Set certificateSet = new HashSet();

	public String findById() {
		errorList = new ArrayList();
		if (invalidate()) {
			this.staffList.add(staff);
			this.setStaffList(staffList);
			this.setErrorList(errorList);
			this.setFamilySet(staff.getStaffFamilies());
			this.setEducationSet(staff.getStaffEducations());
			this.setExperienceSet(staff.getStaffExperiences());
			this.setCertificateSet(staff.getStaffCertificates());
		}
		return "success";
	}

	public String findAll() {
		String hql1 = "select count(*) from Staff";
		String hql2 = "from Staff";
		List list = hqlQuery.find(hql1);
		Long sum = (Long) list.get(0);
		if (page == null)
			page = new PageModel();
		this.page.setLineCount(sum == null ? 0 : sum.intValue());
		this.setStaffList(hqlQuery.find(hql2, page));
		return "success";
	}

	public String showStaff() {
		invalidate();
		this.setFamilySet(staff.getStaffFamilies());
		this.setEducationSet(staff.getStaffEducations());
		this.setExperienceSet(staff.getStaffExperiences());
		this.setCertificateSet(staff.getStaffCertificates());
		return SUCCESS;
	}

	public boolean invalidate() {
		boolean flag = true;
		if (staffId == null || "".equals(staffId)) {
			this.errorList.add("员工编号不能为空！");
			flag = false;
		} else {
			staff = (Staff) baseDAO.findById(this.staffId,
					com.gzbugu.bugusys.fields.VoClasseFields.STAFF);
			if (staff == null) {
				this.errorList.add("编号为" + this.staffId + "的员工不存在！");
				flag = false;
			}
		}
		return flag;
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public List getStaffList() {
		return staffList;
	}

	public void setStaffList(List staffList) {
		this.staffList = staffList;
	}

	public List getErrorList() {
		return errorList;
	}

	public void setErrorList(List errorList) {
		this.errorList = errorList;
	}

	public Set getFamilySet() {
		return familySet;
	}

	public void setFamilySet(Set familySet) {
		this.familySet = familySet;
	}

	public Set getEducationSet() {
		return educationSet;
	}

	public void setEducationSet(Set educationSet) {
		this.educationSet = educationSet;
	}

	public Set getExperienceSet() {
		return experienceSet;
	}

	public void setExperienceSet(Set experienceSet) {
		this.experienceSet = experienceSet;
	}

	public Set getCertificateSet() {
		return certificateSet;
	}

	public void setCertificateSet(Set certificateSet) {
		this.certificateSet = certificateSet;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public PageModel getPage() {
		return page;
	}

	public void setPage(PageModel page) {
		this.page = page;
	}

	public Staff getStaff() {
		return staff;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

}
